<?php
$oJobConfigTypes = JobHandler::getConfigTypes();
$oJobs = JobHandler::getJobs();
$sMissiongroups = $oJobs[$_SESSION['iJobID']]->getMissiongroups();
$oMissions = $oJobs[$_SESSION['iJobID']]->getMissions();
$oUsers = UserHandler::getUsers();
$oJobConfig = $oJobs[$_SESSION['iJobID']]->getConfig();
$iChangers = JobHandler::getUsersWithAccess('mission', 'write');
$oFiles = JobHandler::getFiles($_SESSION['iJobID']);
$oIssues = IssueHandler::getIssues($_SESSION['iJobID']);

function showMission($iMission, $bShowFinishTime, $bShowTotalwork) {
	print '<tr id="holder_job_view_mission_'.$iMission.'">';
	require('include/classes/mission/ui/mission.show_single.ui.php');
	print '</tr>';
	if ($oMissions[$iMission]->iNoteCount > 0) {
		print '<tr id="note_'.$iMission.'">';
		print '</tr>';
	}
}

?>
<script language="JavaScript" src="js/rotmb.js"></script>
<script language="JavaScript" src="js/rotmb_extra.js"></script>
<script language="JavaScript">
	var iNoteMissions = new Array();
	var iNoteMissionCount = 0;
<?php
$bShowFinishTime = false;
$bShowTotalwork = false;
foreach ($oMissions as $oThisMission) {
	if ($oThisMission->iFinishTime) {
		$bShowFinishTime = true;
	}
	
	if ($oThisMission->iTotalwork) {
		$bShowTotalwork = true;
	}
	
	if ($oThisMission->iNoteCount > 0) {
		?>
		iNoteMissions[iNoteMissionCount++] = <?php print $oThisMission->iID ?>;
		<?php
	}
}

?>
	function showNote(iMission) {
		getPiece('note-list', 'iMission=' + iMission, 'note_' + iMission);
	}
	
	function hideNote(iMission) {
		if (document.getElementById('note_' + iMission) != null) {
			document.getElementById('note_' + iMission).innerHTML = '';
		}
	}
	
	function toggleNote(iMission) {
		if (document.getElementById('note_' + iMission).innerHTML == '') {
			showNote(iMission);
		}
		else {
			hideNote(iMission);
		}
	}
	
	function toggleAllNotes(bShow) {
		for (i = 0; i < iNoteMissionCount; i++) {
			if (bShow) {
				showNote(iNoteMissions[i]);
			}
			else {
				hideNote(iNoteMissions[i]);
			}
		}
	}
</script>

<?php
$iColspan = 4;
if ($bShowFinishTime) {
	$iColspan++;
}
if ($bShowTotalwork) {
	$iColspan++;
}
if (count($iChangers) > 1) {
	$iColspan++;
}

$bShowHiddenMissions = (@$_SESSION['oJobSettings'][$_SESSION['iJobID']]['showhiddenmissions'] ? $_SESSION['oJobSettings'][$_SESSION['iJobID']]['showhiddenmissions']  : false);
$oMenuBar = array();
$oMenuBar['mnuFile'] = array(
	'caption' => msg('File'),
	'menu' => array(
		array(
			'type' => 'url',
			'caption' => msg('Create report...'),
			'url' => 'javascript: showBox(\'job-makereport\', \'iJobID='.$_SESSION['iJobID'].'\');',
		),
	),
);
$oMenuBar['mnuView'] = array(
	'caption' => msg('View'),
	'menu' => array(
		array(
			'type' => 'url',
			'caption' => msg('Show all notes'),
			'url' => 'javascript: toggleAllNotes(true); clearMenu()',
		),
		array(
			'type' => 'url',
			'caption' => msg('Hide all notes'),
			'url' => 'javascript: toggleAllNotes(false); clearMenu()',
		),
		array(
			'type' => 'sep',
		),
		array(
			'type' => 'bool',
			'caption' => msg('Show hidden tasks'),
			'checked' => $bShowHiddenMissions,
			'url' => 'index.php?sView=job-view&fJobVar=showhiddenmissions&amp;fJobValue='.($bShowHiddenMissions ? '0' : '1'),
		),
	),
);
if (count($iChangers) > 0) {
	$oMenuBar['mnuUsers'] = array(
		'caption' => msg('User'),
		'menu' => array(
			array(
				'type' => 'bool',
				'caption' => msg('All'),
				'checked' => (@$_SESSION['oJobSettings'][$_SESSION['iJobID']]['showchanger'] == 0),
				'url' => 'index.php?sView='.$_REQUEST['sView'].'&fJobVar=showchanger&fJobValue=0',
			),
			array(
				'type' => 'sep',
			),
		),
	);
}
if (count($sMissiongroups) > 0) {
	$oMenuBar['mnuGroups'] = array(
		'caption' => msg('Taskgroups'),
		'menu' => array(
			array(
				'type' => 'bool',
				'caption' => msg('All'),
				'checked' => ($_SESSION['oJobSettings'][$_SESSION['iJobID']]['onlymissiongroup'] ? false : true),
				'url' => 'index.php?sView=job-view&fJobVar=onlymissiongroup&amp;fJobValue=0',
			),
			array(
				'type' => 'sep',
			),
		),
	);
}
if (JobHandler::canDo('file', 'read')) {
	$oMenuBar['mnuFiles'] = array(
		'caption' => msg('Files'),
		'menu' => array()
	);
	if (JobHandler::canDo('file', 'write')) {
		$oMenuBar['mnuFiles']['menu'][] = array(
			'type' => 'url',
			'caption' => msg('Files...'),
			'url' => 'javascript: showBox(\'job-files\'); clearMenu();',
		);
		$oMenuBar['mnuFiles']['menu'][] = array(
			'type' => 'sep',
		);
	}
}
if (JobHandler::canDo('job', 'write') || JobHandler::canDo('mission', 'write')) {
	$oMenuBar['mnuConfig'] = array(
		'caption' => msg('Settings'),
		'menu' => array(
			array(
				'type' => 'url',
				'caption' => msg('Taskgroups...'),
				'url' => 'javascript: showBox(\'job-missiongroups\'); clearMenu();',
			)
		)
	);
	if (JobHandler::canDo('job', 'write')) {
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'url',
			'caption' => msg('Access...'),
			'url' => 'javascript: showBox(\'job-access\'); clearMenu();',
		);
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'url',
			'caption' => msg('Urgency...'),
			'url' => 'javascript: showBox(\'job-urgencies\'); clearMenu();',
		);
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'url',
			'caption' => msg('Project description...'),
			'url' => 'javascript: showBox(\'job-setdescription\'); clearMenu();',
		);
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'sep',
		);
	}
}

if (count($iChangers) > 1)
{
        foreach ($iChangers as $iThisChanger)
        {
		$oMenuBar['mnuUsers']['menu'][] = array(
			'type' => 'bool',
			'caption' => $oUsers[$iThisChanger]->sUser,
			'checked' => ($_SESSION['oJobSettings'][$_SESSION['iJobID']]['showchanger'] == $iThisChanger),
			'url' => 'index.php?sView='.$_REQUEST['sView'].'&fJobVar=showchanger&fJobValue='.$iThisChanger,
		);
        }
}

if (JobHandler::canDo('job', 'write')) {
	foreach ($oJobConfigTypes as $sThisID => $oThisConfigType) {
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'bool',
			'caption' => $oThisConfigType['caption'],
			'checked' => (@$oJobConfig[$sThisID] ? true : false),
			'url' => 'index.php?sView='.$_REQUEST['sView'].'&sAction=job-setconfig&sVarName='.$sThisID.'&mVarValue='.(@$oJobConfig[$sThisID] ? '0' : '1'),
		);
	}
	if (!$oJobs[$_SESSION['iJobID']]->iOwner) {
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'sep'
		);
		$oMenuBar['mnuConfig']['menu'][] = array(
			'type' => 'url',
			'caption' => 'Gerast eigandi a� verkefni',
			'url' => 'index.php?sView='.$_REQUEST['sView'].'&sAction=job-setowner&iUser='.$_SESSION['rid']
		);
	}
}

foreach ($sMissiongroups as $iThisID => $sThisGroup) {
	$oMenuBar['mnuGroups']['menu'][] = array(
		'type' => 'bool',
		'caption' => $sThisGroup,
		'checked' => ($_SESSION['oJobSettings'][$_SESSION['iJobID']]['onlymissiongroup'] == $iThisID),
		'url' => 'index.php?sView=job-view&fJobVar=onlymissiongroup&amp;fJobValue='.$iThisID,
	);
}
foreach ($oFiles as $oThisFile) {
	$oMenuBar['mnuFiles']['menu'][] = array(
		'type' => 'url',
		'caption' => $oThisFile->sFilename.($oThisFile->sDescription ? '&nbsp;&nbsp;<i>('.$oThisFile->sDescription.')</i>' : ''),
		'url' => 'index.php?sAction=job-filedownload&sFile='.urlencode($oThisFile->sFilename).'&sView=job-view',
		'onclick' => 'clearMenu();',
	);
}

?>

<table class="boxborder" cellspacing="0" style="width: 100%;">
	<tr>
		<td class="boxhandle" colspan="<?php print $iColspan ?>"><?php print $oJobs[$_SESSION['iJobID']]->sName ?></td>
	</tr>
	<tr>
		<td class="control small" colspan="<?php print ($iColspan - 2) ?>" nowrap>

<?php
showMenuBar($oMenuBar);
?>
		</td>
		<td class="control small right nowrap" colspan="2">
			| <?php print msg('Owner') ?>:
<?php
$iOwner = $oJobs[$_SESSION['iJobID']]->iOwner;
if ($iOwner) {
	print '<a href="index.php?sView=user-view&iUser='.$iOwner.'">'.$oUsers[$iOwner]->sUser.'</a>';
}
else {
	print '<span class="error">[ '.msg('Nobody').' ]</span>';
}
?>
		</td>
	</tr>
<?php
$bShowIssueReceivers = false;
foreach ($oIssues as $oThisIssue) {
	if ($oThisIssue->iReceiver) {
		$bShowIssueReceivers = true;
		break;
	}
}

if (count($oIssues) > 0) {
	?>
	<tr>
		<td class="clear" colspan="<?php print $iColspan ?>">
			<table class="clear" style="width: 100%;" cellspacing="0">
				<tr>
					<td class="listtop"><?php print msg('Issues for processing') ?></td>
					<td class="listtop center"><?php print msg('Timing') ?></td>
	<?php
	if ($bShowIssueReceivers) {
		print '<td class="listtop center">'.msg('Receiver').'</td>';
	}
	?>
					<td class="listtop">&nbsp;</td>
				</tr>
	<?php
	foreach ($oIssues as $oThisIssue) {
		?>
				<tr>
					<td class="listentity small">
		<?php
		if (strlen($oThisIssue->sBody) > 80) {
			print substr(strip_tags($oThisIssue->sBody), 0, 80).'...';
		}
		else {
			print $oThisIssue->sBody;
		}
		?>
					</td>
					<td class="listentity small center"><?php print coolDate($oThisIssue->iTiming) ?></td>
		<?php
		if ($bShowIssueReceivers) {
			print '<td class="listentity small center">'.$oUsers[$oThisIssue->iReceiver]->sUser.'&nbsp;</td>';
		}
		?>
					<td class="listentity small center nowrap" style="width: 1px;">
						<input type="button" onClick="showBox('issue-accept', 'iIssue=<?php print $oThisIssue->iID ?>');" value="<?php print msg('Process') ?>">
						<input type="button" onClick="showBox('issue-deny', 'iIssue=<?php print $oThisIssue->iID ?>');" value="<?php print msg('Dismiss') ?>">
					</td>
				</tr>
		<?php
	}
	?>
			</table>
		</td>
	</tr>
	<?php
}
?>
	<tr>
		<td class="listtop" style="text-align: center;">
<?php
if (JobHandler::canDo('mission', 'write') && $oJobConfig['bAllowNogroupMissions'])
{
	?>
	<a href="javascript: showBox('mission-add');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/mission-add.png" border="0"></a>
	<?php
}
else
{
	print '&nbsp;';
}
?>
		</td>
		<td class="listtop"><a href="index.php?sView=job-view&fJobVar=sortby&amp;fJobValue=0"><?php print msg('Task') ?></a></td>
		<td class="listtop"><a href="index.php?sView=job-view&fJobVar=sortby&amp;fJobValue=status"><?php print msg('Status') ?></a></td>
		<td class="listtop center"><a href="index.php?sView=job-view&fJobVar=sortby&amp;fJobValue=urgency"><?php print msg('Urgency') ?></a></td>
<?php
if ($bShowTotalwork)
{
	print '<td class="listtop center">'.msg('Hours').'</td>';
}
if (count($iChangers) > 1)
{
	print '<td class="listtop center">'.msg('Assignment').'</td>';
}
if ($bShowFinishTime)
{
	print '<td class="listtop center">'.msg('Deadline').'</td>';
}
?>
	</tr>
<?php
if (JobHandler::canDo('mission', 'read')) {
	foreach ($oMissions as $oThisMission) {
		if ($oThisMission->iGroup == 0) {
			showMission($oThisMission->iID, $bShowFinishTime, $bShowTotalwork);
		}
	}
	foreach ($sMissiongroups as $iThisID => $sThisGroup) {
		?>
		<tr>
			<td class="separator" colspan="<?php print $iColspan ?>">
		<?php
		if (JobHandler::canDo('mission', 'write')) {
			?>
				<a href="javascript: showBox('mission-add', 'iMissiongroupID=<?php print $iThisID ?>');"><img src="images/<?php print $_SESSION['oUserVars']['images'] ?>/mission-add.png" border="0"></a>
			<?php
		}
		print '&nbsp;';
		if ($_SESSION['oJobSettings'][$_SESSION['iJobID']]['onlymissiongroup'] == $iThisID) {
			print $sThisGroup.' <span class="bold">&lt;-</span>';
		}
		else {
			print '<a href="index.php?sView=job-view&fJobVar=onlymissiongroup&fJobValue='.$iThisID.'">'.$sThisGroup.'</a>';
		}
		?>
			</td>
		</tr>
		<?php
		foreach ($oMissions as $oThisMission) {
			if ($oThisMission->iGroup == $iThisID) {
				showMission($oThisMission->iID, $bShowFinishTime, $bShowTotalwork);
			}
		}
	}
}

?>
</table>

<?php
if (@$_REQUEST['iPopupMission']) {
	?>
	<script language="JavaScript">
		showBox('mission-show', 'iMissionID=<?php print $_REQUEST['iPopupMission'] ?>');
	</script>
	<?php
}
?>
